﻿using IdeaOptics;
using System;

namespace RGB_Sampling
{
    internal class Wrapper_Spect
    {
        private Wrapper wrapper;
        private int Pixels = 0; //>0:已连接光谱
        public string Name { get; set; }
        public string Seq { get; set; }

        public void Connect()
        {
            //if (Pixels > 0)
            //{ //断开光谱仪
            //    Name = "";
            //    Seq = "";
            //    Pixels = 0;
            //    return;
            //}
            Console.WriteLine("Init 光谱仪...");
            //创建实例
            wrapper = new Wrapper();
            //打开光谱仪
            Console.WriteLine("Spectrometer NO.:{0}", wrapper.OpenAllSpectrometers());
            //获得光谱仪名字
            Console.WriteLine("Spectrometer Name:{0}", wrapper.getName(0));
            Name = wrapper.getName(0);
            //获得光谱仪序列号
            Console.WriteLine("Spectrometer Serial:{0}", wrapper.getSerialNumber(0));
            Seq = wrapper.getSerialNumber(0);
            //获得光谱仪像素
            Pixels = wrapper.getNumberOfPixels(0);
            Console.WriteLine("Spectrometer Pixels:{0}", Pixels);
            if (Pixels == 0)
            { //未连接到光谱仪
                Name = "";
                Seq = "";
            }
        }

        public void Sampling()
        {
            if (Pixels == 0) return; //光谱未连接
            //Console.Clear();

            //设置积分时间
            wrapper.setIntegrationTime(0, 8000); //us
            // 设置窗口平滑
            wrapper.setBoxcarWidth(0, 100); //数字递增, 波长往中间靠拢
            //设置平均次数
            wrapper.setScansToAverage(0, 3); //数字递增, 采样速度越慢

            //获取波长
            double[] wavelength = wrapper.getWavelengths(0);
            int p = 0;
            foreach (double i in wavelength)
            {
                //Console.WriteLine("Spectrometer WaveLengths{0}:{1}", p, i);
                p++;
            }
            //获取光谱数据
            double[] spectrum = new double[Pixels];
            spectrum = wrapper.getSpectrum(0);
            Console.WriteLine("\n\n");
            for (int i = 0; i < Pixels; i++)
            {
                //Console.WriteLine("Spectrometer Spectrum{0}:{1}", i, spectrum[i]);
                var seq = (int)spectrum[i] / 10000;
                if(seq > 0) Console.WriteLine("波长: {0}; 强度: {1}", (int)wavelength[i], seq);
            }

            //波长定标系数
            //double[] corrections = wrapper.getWavelengthCorrection(0);
            //int m = 0;
            //foreach (double i in corrections)
            //{
            //Console.WriteLine("Spectrometer Correct{0}:{1}", m, i);
            //    m++;
            //}

            //获取非线性矫正系数数组
            //double[] nonlinearitys = wrapper.getCorrectForDetectorNonlinearity(0);
            //int q = 0;
            //foreach (double i in nonlinearitys)
            //{
            //Console.WriteLine("Spectrometer CorrectForDetectorNonlinearity{0}:{1}", q, i);
            //    q++;
            //}

            //是否支持制冷
            //bool isTEC = wrapper.isTECControl(0);
            //Console.Write("Spectrometer TEC Controller:{0} ", isTEC);
            //if (wrapper.isTECControl(0))
            //{
            //设置制冷温度
            //wrapper.setDetectorSetPointCelsius(0, -20);
            //读取温度
            //    Console.WriteLine("{0}", wrapper.getFeatureControllerThermoElectric(0));
            //}
        }

        public void Close()
        {
            if (Pixels == 0) return; //光谱未连接

            wrapper.closeContinueStrobe(0); //关闭光谱
        }
    }
}
